import 'package:basic_ui/localization/string_hardcoded.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
// ignore:depend_on_referenced_packages
import 'package:flutter_web_plugins/url_strategy.dart';

import 'app.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // 针对web平台，禁用了默认的 # 符号
  usePathUrlStrategy();
  //  注册全局的错误处理程序. 更多信息, see:
  //  https://docs.flutter.dev/testing/errors
  registerErrorHandlers();
  // 程序入口
  runApp(const ProviderScope(
    child: MyApp(),
  ));
}

void registerErrorHandlers() {
  // * 处理 Flutter 中的任何未捕获的错误，比如组件构造错误
  FlutterError.onError = (FlutterErrorDetails details) {
    FlutterError.presentError(details);
    debugPrint(details.toString());
  };
  // * 捕获平台层面的错误（如原生代码抛出的错误）。
  PlatformDispatcher.instance.onError = (Object error, StackTrace stack) {
    debugPrint(error.toString());
    return true;
  };
  // * 当某个 Widget 发生构建失败时，使用此构建函数来显示错误 UI，避免应用崩溃。
  ErrorWidget.builder = (FlutterErrorDetails details) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.red,
        title: Text('An error occurred'.hardcoded),
      ),
      body: Center(child: Text(details.toString())),
    );
  };
}
